package cn.iocoder.yudao.module.enterprise.controller.admin.chat;

import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.module.enterprise.controller.admin.chat.knowledgeBase.KnowledgeBaseChatReqVO;
import cn.iocoder.yudao.module.enterprise.controller.admin.chat.knowledgeBase.KnowledgeBaseChatRespVO;
import cn.iocoder.yudao.module.enterprise.controller.admin.chat.knowledgeBase.KnowledgeBaseDeleteDocsReqVO;
import cn.iocoder.yudao.module.enterprise.controller.admin.chat.knowledgeBase.KnowledgeBaseUploadDocsReqVO;
import com.alibaba.fastjson.JSON;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.security.PermitAll;
import java.util.HashMap;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 知识库问答")
@RestController
@RequestMapping("/enterprise/chat/knowledge-base")
@Validated
public class KnowledgeBaseController {

    private static final String BASE_URL = "http://127.0.0.1:7861";

    @PostMapping("/knowledge_base/upload_docs")
    @Operation(summary = "上传文件到知识库，并/或进行向量化")
    @PermitAll
    public CommonResult<Boolean> uploadFile(@Validated @RequestBody KnowledgeBaseUploadDocsReqVO reqVO) {
        HttpUtil.post(BASE_URL + "/knowledge_base" + "/upload_docs", JSON.toJSONString(reqVO));
        return success(true);
    }

    @GetMapping("/list_knowledge_bases")
    @Operation(summary = "获取知识库列表")
    @PermitAll
    public CommonResult<List<String>> listKnowledgeBases() {
        // 1. 执行请求
        String response = HttpUtil.get(BASE_URL + "/knowledge_base" + "/list_knowledge_bases");
        // 2. 解析返回
        JSONObject jsonObject = JSONUtil.parseObj(response);
        JSONArray data = jsonObject.getJSONArray("data");
        return success(data.toList(String.class));
    }

    @GetMapping("/list_files")
    @Operation(summary = "获取知识库内的文件列表")
    @PermitAll
    public CommonResult<List<String>> listFiles(@RequestParam("knowledgeBaseName") String knowledgeBaseName) {
        // 1. 构建请求参数
        HashMap<String, Object> paramMap = new HashMap<>();
        paramMap.put("knowledge_base_name", knowledgeBaseName);
        // 2. 执行请求
        String response = HttpUtil.get(BASE_URL + "/knowledge_base" + "/list_files", paramMap);
        // 3. 解析返回
        JSONObject jsonObject = JSONUtil.parseObj(response);
        JSONArray data = jsonObject.getJSONArray("data");
        return success(data.toList(String.class));
    }

    @DeleteMapping("/delete_docs")
    @Operation(summary = "删除知识库内指定文件")
    @PermitAll
    public CommonResult<Boolean> deleteDocs(@Validated @RequestBody KnowledgeBaseDeleteDocsReqVO reqVO) {
        HttpUtil.post(BASE_URL + "/knowledge_base" + "/delete_docs", JSON.toJSONString(reqVO));
        return success(true);
    }

    @PostMapping("/knowledge_base_chat")
    @Operation(summary = "与知识库对话")
    @PermitAll
    public CommonResult<KnowledgeBaseChatRespVO> localDocChat(@Validated @RequestBody KnowledgeBaseChatReqVO reqVO) {
        // 1. 执行请求
        String response = HttpUtil.post(BASE_URL + "/chat" + "/knowledge_base_chat", JSON.toJSONString(reqVO));
        // 2. 解析返回
        JSONObject jsonObject = JSONUtil.parseObj(response.substring(response.indexOf("{")));
        String answer = jsonObject.getStr("answer");
        JSONArray docs = jsonObject.getJSONArray("docs");
        KnowledgeBaseChatRespVO respVO = new KnowledgeBaseChatRespVO();
        respVO.setResponse(answer);
        respVO.setDocs(docs.toList(String.class));
        return success(respVO);
    }
}
